package com.rongc.client.freight.business.mine.view.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.pay.DoAlipay;
import com.rongc.client.core.pay.alipay.util.OrderInfoUtil2_0;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.core.utils.SystemUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.model.DicsBean;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.base.view.widget.SpaceItemDecoration;
import com.rongc.client.freight.business.mine.view.adapter.RecyclerChongzhiAdapter;
import com.rongc.client.freight.business.mine.view.adapter.RecyclerKeyboardAdapter;
import com.rongc.client.freight.business.mine.view.popup.MinePopup;
import com.rongc.client.freight.business.supply.view.popup.SupplyPopup;
import com.rongc.client.freight.business.waybill.request.api.WaybillPayAliApi;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * Created by Bruce on 2017/7/28.
 */
public class ChongzhiActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.tv_account)
    TextView mTvAccount;
    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;
    RecyclerChongzhiAdapter mAdapter;
    List<DicsBean.DicBean> datas;
    List<String> pwdDatas = new ArrayList<>();
    String price;

    PopupWindow popupWindowPay,popupWindowPayWay;

    Response.Listener<JSONObject> respAliPayListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                popupWindowPay.dismiss();
                Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(response);
                String orderParam = OrderInfoUtil2_0.buildOrderParam(params);

                String sign = OrderInfoUtil2_0.getSign(params);
                final String orderInfo = orderParam + "&" + sign;
                new DoAlipay().payAlipay(orderInfo, ChongzhiActivity.this, new DoAlipay.OnPayCompleteResyltListener() {
                    @Override
                    public void OnResult(String resultStr, String txt, boolean result) {
                        if(result){
                            //  支付成功
                            ActivityUtils.toast("支付成功");

//                            finish();
                        }else{
                            //  支付失败
                            ActivityUtils.toast("支付失败");

                        }
                    }
                });

            } else {
                UtilMethod.dismissProgressDialog(ChongzhiActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(ChongzhiActivity.this);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_pay:
                WaybillPayAliApi pay = new WaybillPayAliApi(new WaybillPayAliApi.WaybillPayParams(
                        price),
                        respAliPayListener, errorListener);
                RequestManager.getInstance().call(pay);
                popupWindowPay.dismiss();

                break;
            case R.id.iv_close:
                popupWindowPay.dismiss();
                break;
            case R.id.lyt_way:
                popupWindowPayWay.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);;
                break;
            case R.id.lyt_wx:
                ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_pay)).setText("微信");
                popupWindowPayWay.dismiss();
                break;
            case R.id.lyt_zfb:
                ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_pay)).setText("支付宝");
                popupWindowPayWay.dismiss();
                break;
            case R.id.lyt_qb:
                ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_pay)).setText("我的钱包");
                popupWindowPayWay.dismiss();
                break;
        }
    }

    public void makePayPopup(){
        final RecyclerKeyboardAdapter mAdapter = new RecyclerKeyboardAdapter(this,pwdDatas);
        popupWindowPay = MinePopup.makePopupPayOption(this, this);
        popupWindowPay.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow()
                        .getAttributes();
                lp.alpha = 1f; // 0.0-1.0
                getWindow().setAttributes(lp);
            }
        });
    }

    public void makePayWayPopup(){
        popupWindowPayWay = SupplyPopup.makePopupPayWayOption(this,this);
        popupWindowPayWay.getContentView().findViewById(R.id.lyt_qb).setVisibility(View.INVISIBLE);
    }
    @Override
    public void initView() {
        initToolbar(R.string.chongzhi_title);
        datas = new ArrayList<>();
        datas.add(new DicsBean.DicBean("50","50元"));
        datas.add(new DicsBean.DicBean("100","100元"));
        datas.add(new DicsBean.DicBean("200","200元"));
        datas.add(new DicsBean.DicBean("500","500元"));
        datas.add(new DicsBean.DicBean("1000","1000元"));
        datas.add(new DicsBean.DicBean("2000","2000元"));
        mAdapter = new RecyclerChongzhiAdapter(this,datas);
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(this,3);
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(SystemUtil.dip2px(this,10f)));
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<DicsBean.DicBean>() {
            @Override
            public void onItemClick(View view, int position, DicsBean.DicBean data) {
                //  支付弹框
                pwdDatas.clear();
                price = data.getKey();
                ((TextView)popupWindowPay.getContentView().findViewById(R.id.tv_total)).setText(
                        String.format(getResources().getString(R.string.chongzhi_popup_total),data.getValue()));

                popupWindowPay.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
                ;
                WindowManager.LayoutParams lp = getWindow()
                        .getAttributes();
                lp.alpha = 0.5f; // 0.0-1.0
                getWindow().setAttributes(lp);

            }
        });


        mTvAccount.setText(String.format(getResources().getString(R.string.chongzhi_acount_lable),
                UniApplication.getInstance().getUserInfo().getPhone()));

        makePayPopup();
        makePayWayPopup();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_chongzhi;
    }
}
